﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using XYX.Print.Client.PrintUI;
using XYX.Print.Models;

namespace XYX.Print.Client
{
    public partial class ReportDesignForm : UIForm
    {
        /// <summary>
        /// 设计器
        /// </summary>

        private string designFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GridDlls", "grdesigner6.exe");

        /// <summary>
        /// 目示
        /// </summary>
        private string barCodeTemplates = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "BarCodeTemplate");
        /// <summary>
        /// 构造
        /// </summary>
        public ReportDesignForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReportDesignForm_Load(object sender, EventArgs e)
        {
            BindListBox();
        }
        /// <summary>
        /// 绑定标签
        /// </summary>

        private void BindListBox()
        {


            string[] files = Directory.GetFiles(barCodeTemplates, "*.grf");
            List<BarCodeInfo> barCodeInfos = new List<BarCodeInfo>();
            foreach (string file in files)
            {
                barCodeInfos.Add(new BarCodeInfo()
                {
                    Id = 1,
                    Name = new FileInfo(file).Name,
                    Path = file,
                });


                this.uiListBox.DataSource = barCodeInfos;
                this.uiListBox.DisplayMember = nameof(BarCodeInfo.Fomart);  // 显示内容  数据的属性
                this.uiListBox.ValueMember = nameof(BarCodeInfo.Id); // 项的值  数据的属性

            }

        }
        private void btn_print_Click(object sender, EventArgs e)
        {
            BatteryPrintForm batteryPrintForm = new BatteryPrintForm();
            batteryPrintForm.Show();
        }

        /// <summary>
        /// 打开模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenBarCodeTemplateDir_Click(object sender, EventArgs e)
        {
            if (!File.Exists(designFile))
            {
                MessageBox.Show("无报表工具");
            }
            BarCodeInfo selecrItem = (BarCodeInfo)uiListBox.SelectedItem;
            if (selecrItem != null)
            {
                Process.Start(designFile, selecrItem.Path);
            }
            else
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件夹";
                dialog.Filter = "所有文件(*.*)|*.*";
                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string file = dialog.FileName;
                    Process.Start(designFile, file);
                }
            }
           
        }
    }
}
